<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<div style="padding:10px">
	<div style="padding-left:10px">
		<table class="form-table">
<!-- 			<tr> -->
<!-- 						<td> -->
<!-- 							<label>id:</label> -->
<!-- 						</td> -->
<!-- 						<td> -->
<!-- 							<input name="id" class="easyui-validatebox" required="true"> -->
<!-- 						</td> -->
<!-- 				<td> -->
<!-- 					<label>单位ID:</label> -->
<!-- 				</td> -->
<!-- 				<td> -->
<!-- 					<select id="unit_id_form" name="unit_id" class="easyui-combotree" required="true" /> -->
<!-- 				</td> -->
<!-- 			</tr> -->
			<tr>
				<td>
					<label>单据号码:</label>
				</td>
				<td>
					<input name="inout_order_number" class="easyui-validatebox" required="true">
				</td>
				<td>
<!-- 						购买入库111；建账入库112； 退货入库T01； -->
					<label>入库类型:</label>
				</td>
				<td>
<!-- 							<input name="physic_in_sort" class="easyui-validatebox" required="true"> -->
					<select id="physic_in_sort_form" name="physic_in_sort" class="easyui-combotree" required="true" />
				</td>
				<td>
					<label>供货单位:</label>
				</td>
				<td>
					<input name="deliverer" class="easyui-validatebox" required="true">
				</td>
			</tr>
			<tr>
				<td>
					<label>出入库日期:</label>
				</td>
				<td>
					<input name="physic_io_date" class="easyui-datebox" required="true">
				</td>
				<td>
					<label>凭据说明:</label>
				</td>
				<td>
					<input name="evidence" class="easyui-validatebox" >
				</td>
				<td>
					<label>付款说明:</label>
				</td>
				<td>
					<input name="account_receivable" class="easyui-validatebox" >
				</td>
			</tr>
			<tr>
				<td>
					<label>付款方式:</label>
				</td>
				<td>
<!-- 							<input name="method_of_payment" class="easyui-validatebox" required="true"> -->
					<select id="method_of_payment_form" name="method_of_payment" class="easyui-combotree" required="true" />
				</td>
				<td>
					<label>合同号:</label>
				</td>
				<td>
					<input name="contract_order_number" class="easyui-validatebox" required="true">
				</td>
				<td>
					<label>发票号:</label>
				</td>
				<td>
					<input name="receipt_number" class="easyui-validatebox" required="true">
				</td>
			</tr>
			<tr>
<!-- 				<td> -->
<!-- 						0 未入帐；1 已入帐: -->
<!-- 					<label>入账标志</label> -->
<!-- 				</td> -->
<!-- 				<td> -->
<!-- 					<select id="account_sign_form" name="account_sign" class="easyui-combotree" required="true" /> -->
<!-- 				</td> -->
				<td>
					<label>税率:</label>
				</td>
				<td>
					<input name="tax_rate" class="easyui-validatebox" required="true">
				</td>
				<td>
					<label>经手人:</label>
				</td>
				<td>
					<input name="brokerage" class="easyui-validatebox" required="true">
				</td>
				<td>
				</td>
				<td>
				</td>
			</tr>
<!-- 					<tr> -->
<!-- 						<td> -->
<!-- 							<label>操作员:</label> -->
<!-- 						</td> -->
<!-- 						<td> -->
<!-- 							<input name="handle" class="easyui-validatebox" required="true"> -->
<!-- 						</td> -->
<!-- 						<td> -->
<!-- 							<label>操作日期:</label> -->
<!-- 						</td> -->
<!-- 						<td> -->
<!-- 							<input name="handle_date" class="easyui-validatebox" required="true"> -->
<!-- 						</td> -->
<!-- 					</tr> -->
<!-- 					<tr> -->
<!-- 						<td> -->
<!-- 							<label>审核员:</label> -->
<!-- 						</td> -->
<!-- 						<td> -->
<!-- 							<input name="account_person" class="easyui-validatebox" required="true"> -->
<!-- 						</td> -->
<!-- 						<td> -->
<!-- 							<label>审核日期:</label> -->
<!-- 						</td> -->
<!-- 						<td> -->
<!-- 							<input name="account_date" class="easyui-validatebox" required="true"> -->
<!-- 						</td> -->
<!-- 					</tr> -->
		</table>
	</div>
	<div style="margin:10px 0;">
		<table id="dg-details" style="height:180px;" 
			url='${path}/psPhysicInDetail/getByIONum'
		>
			<thead>
				<tr>
					<th field="id" width="100" hidden="true">药品编码</th>
					<th data-options="field:'physic_code',title:'药品编码',width:100,
			        	formatter:function(value,row){
							return row.physic_name;
						},
			        	editor:{
			        		type:'combobox',
			        		options:{
			        			valueField:'physic_code',
			        			textField:'physic_name',
								panelHeight: '120',
			        			required:true,
								onSelect: function(rec){
									var pu_ed = $('#dg-details').datagrid('getEditor', {index:editIndex,field:'physic_unit'});
									$(pu_ed.target).combobox('setText',rec['physic_unit']);
									var ps_ed = $('#dg-details').datagrid('getEditor', {index:editIndex,field:'physic_spec'});
									$(ps_ed.target).combobox('setText',rec['physic_spec']);
									var ps_ed = $('#dg-details').datagrid('getEditor', {index:editIndex,field:'physic_form'});
									$(ps_ed.target).combobox('setText',rec['physic_form']);
									
									//刷新行
		// 							$('#dg-details').datagrid('refreshRow', editIndex);
								}
			        		}
			        	}">药品编码</th>
					<th field="inout_order_number" width="100">单据号码</th>
<!-- 					购买入库111；建账入库112； 退货入库T01； -->
					<th field="physic_in_sort" width="100">入库类型</th>
					<th field="deliverer" width="100">供货单位</th>
					<th field="physic_io_date" width="100">出入库日期</th>
					<th field="evidence" width="100">凭据说明</th>
					<th field="account_receivable" width="100">付款说明</th>
					<th field="method_of_payment" width="100">付款方式</th>
					<th field="contract_order_number" width="100">合同号</th>
					<th field="receipt_number" width="100">发票号</th>
					<th field="handle" width="100">操作员</th>
					<th field="handle_date" width="100">操作日期</th>
					<th field="account_sign" width="100">0 未入帐；1 已入帐</th>
					<th field="account_person" width="100">审核员</th>
					<th field="account_date" width="100">审核日期</th>
					<th field="tax_rate" width="100">税率</th>
					<th field="brokerage" width="100">经手人</th>
				</tr>
			</thead>
		</table>
	</div>
</div>
<input id="inserted" name="inserted" type="hidden"></input>
<input id="updated" name="updated" type="hidden"></input>
<input id="deleted" name="deleted" type="hidden"></input>

<script type="text/javascript">
	$('#dg-details').datagrid({
// 		editable: ($('#bill-editable').val()=='true'),
		url:'${path}/psPhysicInDetail/getByIONum',  
		columns:[[
	        {field:'physic_code',title:'药品编码',width:100,
	        	formatter:function(value,row){
					return row.physic_name;
				},
	        	editor:{
	        		type:'combobox',
	        		options:{
	        			valueField:'physic_code',
	        			textField:'physic_name',
						panelHeight: '120',
	        			required:true,
						onSelect: function(rec){
							var pu_ed = $('#dg-details').datagrid('getEditor', {index:editIndex,field:'physic_unit'});
							$(pu_ed.target).combobox('setText',rec['physic_unit']);
							var ps_ed = $('#dg-details').datagrid('getEditor', {index:editIndex,field:'physic_spec'});
							$(ps_ed.target).combobox('setText',rec['physic_spec']);
							var ps_ed = $('#dg-details').datagrid('getEditor', {index:editIndex,field:'physic_form'});
							$(ps_ed.target).combobox('setText',rec['physic_form']);
							
							//刷新行
// 							$('#dg-details').datagrid('refreshRow', editIndex);
						}
	        		}
	        	}
	        },  
// 	        {field:'physic_name',title:'药品名称',width:100},   
	        {field:'physic_spec',title:'规格',width:100,
	        	formatter:function(value,row){
					return row.physic_spec_name;
				},
	        	editor:{
	        		type:'combobox',
	        		options:{
	        			valueField:'code',
	        			textField:'name',
						panelHeight: '120',
						disabled: true,
						editable: false,
						hasDownArrow: false,	//没有下来按钮
	        			required:true
// 	        			method:'get',
// 	        			url:path+'/sysDict/kvByType?pid=药品规格'
	        		}
	        	}
	        },  
	        {field:'physic_unit',title:'单位',width:100,
	        	formatter:function(value,row){
					return row.physic_unit_name;
				},
	        	editor:{
	        		type:'combobox',
	        		options:{
	        			valueField:'code',
	        			textField:'name',
						panelHeight: '120',
	        			required: true
	        		}
	        	}
	        },  
	        {field:'physic_form',title:'剂型',width:100,
	        	formatter:function(value,row){
					return row.physic_form_name;
				},
	        	editor:{
	        		type:'combobox',
	        		options:{
	        			valueField:'code',
	        			textField:'name',
						panelHeight: '120',
	        			required:true
// 	        			method:'get',
// 	        			url:path+'/sysDict/kvByType?pid=药品剂型'
	        		}
	        	}
	        },  
	        {field:'physic_batch_code',title:'批号',width:100,
	        	editor:{
	        		type:'text'
	        	}
	        },
	        {field:'quantity',title:'数量',width:100,editor:'numberbox'},  
	        {field:'in_price',title:'进货价',width:100,editor:{type:'numberbox',options:{precision:2}}},
	        {field:'xx',title:'',width:100,align:'right'}  
	    ]],
	    toolbar: [{
			iconCls: 'icon-add',
			text: '增加',
			handler: function(){addDetail();}
		},'-',{
			iconCls: 'icon-remove',
			text: '删除',
			handler: function(){delDetail();}
		}
// 		,'-',{
// 			iconCls: 'icon-save',
// 			text: '保存',
// 			handler: function(){saveDetail();}
// 		}
		,'-',{
			iconCls: 'icon-undo',
			text: '撤销',
			handler: function(){rejectDetail();}
		},'-',{
			iconCls: 'icon-search',
			text: '查看修改行',
			handler: function(){getChanges();}
		}],  //表格菜单
//         fit:true,
		onClickRow: onClickRow,
        fitColumns: false,
        loadMsg:'加载中...', //加载提示
        rownumbers:true, //显示行号列
        striped: true,
        singleSelect:true,//是允许选择一行
//         onClickCell: onClickCell,
        queryParams:{   //在请求数据是发送的额外参数，如果没有则不用写
        },
        onLoadSuccess:function(data){
        },
        rowStyler:function(index,row){
        },  
		onAfterEdit: function (rowIndex, rowData, changes) {//编辑完触发    
		}
	});
	var editIndex = undefined;
	function endEditing(){
		if (editIndex == undefined){return true}
		if ($('#dg-details').datagrid('validateRow', editIndex)){
			//设置选择框 所在列的 值 为 text
			var physic_spec_ed = $('#dg-details').datagrid('getEditor', {index:editIndex,field:'physic_spec'});
			var physic_spec_name = $(physic_spec_ed.target).combobox('getText');
			$('#dg-details').datagrid('getRows')[editIndex]['physic_spec_name'] = physic_spec_name;
			
			var physic_unit_ed = $('#dg-details').datagrid('getEditor', {index:editIndex,field:'physic_unit'});
			var physic_unit_name = $(physic_unit_ed.target).combobox('getText');
			$('#dg-details').datagrid('getRows')[editIndex]['physic_unit_name'] = physic_unit_name;
			
			var physic_form_ed = $('#dg-details').datagrid('getEditor', {index:editIndex,field:'physic_form'});
			var physic_form_name = $(physic_unit_ed.target).combobox('getText');
			$('#dg-details').datagrid('getRows')[editIndex]['physic_form_name'] = physic_form_name;
			
			var physi_name_ed = $('#dg-details').datagrid('getEditor', {index:editIndex,field:'physic_code'});
			var physic_name = $(physi_name_ed.target).combobox('getText');
			$('#dg-details').datagrid('getRows')[editIndex]['physic_name'] = physic_name;
			
			
			$('#dg-details').datagrid('endEdit', editIndex);
			editIndex = undefined;
			return true;
		} else {
			return false;
		}
	}
	function onClickRow(index,rowData){
		if (editIndex != index){
			if (endEditing()){
				$('#dg-details').datagrid('selectRow', index)
						.datagrid('beginEdit', index);
				editIndex = index;
				loadData();
			} else {
				$('#dg-details').datagrid('selectRow', editIndex);
			}
		}
	}
	// 添加
	function addDetail(){
		if (endEditing()){
			$('#dg-details').datagrid('appendRow',{quantity: 0});
			editIndex = $('#dg-details').datagrid('getRows').length-1;
			
			$('#dg-details').datagrid('selectRow', editIndex)
					.datagrid('beginEdit', editIndex);
			loadData();
		}
	}
	//加载数据
	function loadData(){
			var editor = $('#dg-details').datagrid('getEditor', {index: editIndex,field:'physic_spec'});//取当前的编辑器
			//rklxData使用前必须解析成对象
			$(editor.target).combobox("loadData", ypggData);
			
			var editor = $('#dg-details').datagrid('getEditor', {index: editIndex,field:'physic_unit'});//取当前的编辑器
			//rklxData使用前必须解析成对象
			$(editor.target).combobox("loadData", ypdwData);
			
			var editor = $('#dg-details').datagrid('getEditor', {index: editIndex,field:'physic_form'});//取当前的编辑器
			//rklxData使用前必须解析成对象
			$(editor.target).combobox("loadData", ypjxData);
			
			var physicCodeEditor = $('#dg-details').datagrid('getEditor', {index: editIndex,field:'physic_code'});//取当前的编辑器
			//rklxData使用前必须解析成对象
 			$(physicCodeEditor.target).combobox("loadData", physicData);
			
	}
	//删除
	function delDetail() {
	    //删除时先获取选择行
	    var rows = $('#dg-details').datagrid("getSelections");
	    //选择要删除的行
	    if (rows.length > 0) {
	        $.messager.confirm("提示", "你确定要删除吗?", function (r) {
	            if (r) {
	                var ids = [];
	                for (var i = 0; i < rows.length; i++) {
	                    ids.push(rows[i].physic_code);
	                }
	                //将选择到的行存入数组并用,分隔转换成字符串，
	                //本例只是前台操作没有与数据库进行交互所以此处只是弹出要传入后台的id
	                alert(ids.join(','));
			        //页面删除
					if (editIndex == undefined){return}
					$('#dg-details').datagrid('cancelEdit', editIndex)
							.datagrid('deleteRow', editIndex);
					editIndex = undefined;
	            }
	        });
	    }
	    else {
	        $.messager.alert("提示", "请选择要删除的行", "error");
	    }
	}
	//保存
	function saveDetail(){
		if (endEditing()){
			if ($('#dg-details').datagrid('getChanges').length) {
				var inserted = $('#dg-details').datagrid('getChanges', 'inserted');
				var updated = $('#dg-details').datagrid('getChanges', 'updated');
				var deleted = $('#dg-details').datagrid('getChanges', 'deleted');
				var effectRow = new Object();
				if (inserted.length) { 
					effectRow["inserted"] = JSON.stringify(inserted); 
				}
				if (updated.length) {
					effectRow["updated"] = JSON.stringify(updated); 
				}
				if (deleted.length) {
					effectRow["deleted"] = JSON.stringify(deleted); 
				}
				$.post( 
					path+"/psPhysicIn/add", 
					effectRow,
					function(data,status){
						alert(data.msg);
						if(data.success){
							$('#dg-details').datagrid('acceptChanges');
						}
					}
				);
			}
		}
	}
	//撤销
	function rejectDetail(){
		$('#dg-details').datagrid('rejectChanges');
		editIndex = undefined;
	}
	function getChanges(){
// 		alert("获得修改数据");
		var rows = $('#dg-details').datagrid('getChanges');
		alert(rows.length+' 修改!');
	}
</script>
